package io.gitee.zhangbinhub.acp.boot.conf;

import io.gitee.zhangbinhub.acp.boot.constant.AcpCoreConstant;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.ArrayList;
import java.util.List;

@ConfigurationProperties(prefix = AcpCoreConstant.CORS_PREFIX)
public class AcpCorsConfiguration {
    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public List<String> getAllowedOrigins() {
        return allowedOrigins;
    }

    public void setAllowedOrigins(List<String> allowedOrigins) {
        this.allowedOrigins = allowedOrigins;
    }

    public List<String> getAllowedMethods() {
        return allowedMethods;
    }

    public void setAllowedMethods(List<String> allowedMethods) {
        this.allowedMethods = allowedMethods;
    }

    public List<String> getAllowedHeaders() {
        return allowedHeaders;
    }

    public void setAllowedHeaders(List<String> allowedHeaders) {
        this.allowedHeaders = allowedHeaders;
    }

    public List<String> getExposedHeaders() {
        return exposedHeaders;
    }

    public void setExposedHeaders(List<String> exposedHeaders) {
        this.exposedHeaders = exposedHeaders;
    }

    public Boolean getAllowCredentials() {
        return allowCredentials;
    }

    public void setAllowCredentials(Boolean allowCredentials) {
        this.allowCredentials = allowCredentials;
    }

    public Integer getMaxAge() {
        return maxAge;
    }

    public void setMaxAge(Integer maxAge) {
        this.maxAge = maxAge;
    }

    private Boolean enabled = false;
    private List<String> allowedOrigins = new ArrayList<>();
    private List<String> allowedMethods = new ArrayList<>();
    private List<String> allowedHeaders = new ArrayList<>();
    private List<String> exposedHeaders = new ArrayList<>();
    private Boolean allowCredentials = true;
    private Integer maxAge = 3600;

    private CorsConfiguration getCorsConfiguration() {
        CorsConfiguration config = new CorsConfiguration();
        allowedOrigins.forEach(config::addAllowedOrigin);
        config.setMaxAge(maxAge.longValue());
        config.setAllowCredentials(allowCredentials);
        if (allowedMethods.isEmpty()) {
            config.addAllowedMethod("*");
        } else {
            config.setAllowedMethods(allowedMethods);
        }
        if (allowedHeaders.isEmpty()) {
            config.addAllowedHeader("*");
        } else {
            config.setAllowedHeaders(allowedHeaders);
        }
        if (exposedHeaders.isEmpty()) {
            config.addExposedHeader("*");
        } else {
            config.setExposedHeaders(exposedHeaders);
        }
        return config;
    }

    public CorsConfigurationSource getServletCorsConfigurationSource() {
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**", getCorsConfiguration());
        return configurationSource;
    }

    public org.springframework.web.cors.reactive.CorsConfigurationSource getReactiveCorsConfigurationSource() {
        org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource configurationSource = new org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**", getCorsConfiguration());
        return configurationSource;
    }
}
